package org.stone.project.securityInterface.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import lombok.Getter;
import org.stone.project.common.pojo.exception.BuzException;
import org.stone.project.securityInterface.annotation.PermissionCheck;
import org.stone.project.securityInterface.pojo.dto.PermissionAnnotationDto;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 权限注解校验工具
 */
public class PermissionAnnotationCheckUtil {
    /**
     * 方法名称
     */
    private static List<String> methodNameList;

    /**
     * 权限map
     */
    private static Map<String, PermissionAnnotationDto> permissionMap;

    static {
        Method[] methods = ReflectUtil.getMethodsDirectly(PermissionsCheckUtil.class,false,false);
        methodNameList = Arrays.stream(methods)
                .map(ele -> ele.getName())
                .collect(Collectors.toList());
    }

    public static PermissionAnnotationDto getPermissionDto(String value){
        return permissionMap.get(value);
    }

    /**
     * 注解对象解析
     * @param permissionCheck
     * @return
     */
    public static PermissionAnnotationDto tranferDto(PermissionCheck permissionCheck) {
        PermissionAnnotationDto permissionAnnotationDto = new PermissionAnnotationDto();
        String errMsg = null;
        String checkStr = permissionCheck.value();
        String regex = "(\\w+)\\(([^)]*)\\)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(checkStr);

        permissionAnnotationDto.setAnnotationStr(checkStr);
        permissionAnnotationDto.setCheckStatus(false);

        if (matcher.find()) {
            String methodName = matcher.group(1);
            String parameters = matcher.group(2);
            String[] paramArray = parameters.split(",\\s*");
            permissionAnnotationDto.setParams(CollectionUtil.toList(paramArray));

            boolean hasName = methodNameList.stream()
                    .anyMatch(ele -> ele.equals(methodName));
            if (hasName){
                if (paramArray.length == 0){
                    errMsg = "参数不能为空";
                }else{
                    permissionAnnotationDto.setCheckStatus(true);
                    permissionAnnotationDto.setMethod(ReflectUtil.getMethod(PermissionsCheckUtil.class, methodName));
                }
            }else
                errMsg = "方法名称格式错误";
        }else {
            errMsg = "权限注解格式错误";
        }

        return permissionAnnotationDto;
    }

    /**
     * 添加注解权限信息
     */
    public static void addMenu(String permissionStr,PermissionAnnotationDto permissionAnnotationDto){
        if (ObjectUtil.isNull(permissionMap.get(permissionStr))){
            permissionMap.put(permissionStr,permissionAnnotationDto);
        }else
            throw new BuzException("名称为"+permissionStr+"权限重复");
    }
}
